package pro.shuangxi.source;

import cn.hutool.core.date.format.FastDateFormat;
import cn.hutool.core.text.NamingCase;
import pro.shuangxi.anno.Comment;
import pro.shuangxi.utils.StringUtils;

import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.Table;
import java.lang.reflect.Field;
import java.util.*;

/**
 * @author mengshx
 **/
public class JpaClazzMapSource implements MapSource{
    Class<?> clazz=null;
    Map<String, Object> mapData = new HashMap<>();
    public JpaClazzMapSource(Class clazz) {
        this.clazz=clazz;
    }


    @Override
    public Map<String, Object> getMap() {


        Table annotation = clazz.getAnnotation(Table.class);
        if (annotation == null) throw new RuntimeException("起码标个Table注解告诉我表名吧");
        Comment tableComment = clazz.getAnnotation(Comment.class);
        if (tableComment != null) {
            mapData.put("tableComment", tableComment.value());
        } else {
            mapData.put("tableComment", "");
        }
        String table_name = annotation.name();
        mapData.put("table_name", table_name);
        mapData.put("table-name", NamingCase.toKebabCase(NamingCase.toPascalCase(table_name)));
        mapData.put("tableName", StringUtils.toCamelCase(table_name));
        mapData.put("TableName", StringUtils.convertToCamelCase(table_name));
        mapData.put("date", FastDateFormat.getInstance().format(new Date()));
        Field[] fields = clazz.getDeclaredFields();
        List<Map<String, Object>> columns = new ArrayList<>();
        for (int i = 0; i < fields.length; i++) {
            Field field = fields[i];
            Column columnAnno = field.getAnnotation(Column.class);
            if (columnAnno != null) {
                String column_name = columnAnno.name();
                Map<String, Object> column = new HashMap<>();
                columns.add(column);
                Id IdAnno = field.getAnnotation(Id.class);
                if (IdAnno != null) {
                    column.put("isPk", "1");
                    mapData.put("key_name", column_name);
                    mapData.put("keyName", NamingCase.toCamelCase(column_name));
                    mapData.put("KeyName", NamingCase.toPascalCase(column_name));
                } else {
                    column.put("isPk", "0");

                }
                Comment comment = field.getAnnotation(Comment.class);
                if (comment != null) {
                    column.put("columnComment", comment.value());
                } else {
                    column.put("columnComment", "");

                }
                column.put("column_name", column_name);
                column.put("COLUMN_NAME", column_name.toUpperCase());
                column.put("column-name", NamingCase.toKebabCase(NamingCase.toPascalCase(column_name)));

                column.put("ColumnName", StringUtils.convertToCamelCase(column_name));
                column.put("COLUMNNAME", column_name.toUpperCase().replace("_", ""));
                column.put("columnName", StringUtils.toCamelCase(column_name));
                column.put("isRequired", null);
                column.put("sort", i);
                column.put("isIncrement", "");
                if (field.getGenericType() == Long.class) {
                    column.put("javaType", "Long");
                    column.put("jsType", "number");

                }
                if (field.getGenericType() == Integer.class) {
                    column.put("javaType", "Integer");
                    column.put("jsType", "number");
                }
                if (field.getGenericType() == Double.class) {
                    column.put("javaType", "Double");
                    column.put("jsType", "number");
                }
                if (field.getGenericType() == Date.class) {
                    column.put("javaType", "Date");
                    column.put("jsType", "string");
                }
                if (field.getGenericType() == String.class) {
                    column.put("javaType", "String");
                    column.put("jsType", "string");
                }
                if (field.getGenericType() == Boolean.class) {
                    column.put("javaType", "Boolean");
                    column.put("jsType", "boolean");
                }
            }

        }
        mapData.put("columns", columns);
        return mapData;
    }
}
